package com.estagio.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import util.HibernateUtil;

import com.estagio.entity.Estado;
import com.estagio.entity.Matricula;

public class MatriculaDao extends Dao<Matricula>{

	private Session sessao;


	public Session getSessao() {
		return sessao;
	}

	public void setSessao(Session sessao) {
		this.sessao = sessao;
	}

	@Override
	public List<Matricula> find(Object obj) {
		Matricula matricula = (Matricula) obj;
		this.sessao =  HibernateUtil.getSession().getCurrentSession();
		this.list();
		List<Matricula> lista;
		Criteria criteria = this.sessao.createCriteria(Matricula.class);
		addRestrictionIfNotNull(criteria, "dtMatricula", matricula.getDtMatricula());
		if((matricula.getAluno() != null) && (matricula.getAluno().getIdAluno() != null) && (matricula.getAluno().getIdAluno() != 0)) {
			addRestrictionIfNotNull(criteria, "aluno.idAluno", matricula.getAluno().getIdAluno());
		}
		if((matricula.getAluno() != null) && (matricula.getAluno().getNome() != null) && (!matricula.getAluno().getNome().equals(""))) {
			criteria.createCriteria("aluno", "Aluno", Criteria.INNER_JOIN).add(Restrictions.like("nome", matricula.getAluno().getNome(), MatchMode.ANYWHERE).ignoreCase());
		}
		
		if((matricula.getPlano() != null) && (matricula.getPlano().getIdPlano() != null) && (matricula.getPlano().getIdPlano() != 0)) {
			addRestrictionIfNotNull(criteria, "plano.idPlano", matricula.getPlano().getIdPlano());
		}
		if((matricula.getCondicaoPagamento() != null) && ((matricula.getCondicaoPagamento().getIdCondicaoPagamento() != null) && (matricula.getCondicaoPagamento().getIdCondicaoPagamento() != 0))) {
			addRestrictionIfNotNull(criteria, "condicaoPagamento.idCondicaoPagamento", matricula.getCondicaoPagamento().getIdCondicaoPagamento());
		}		
		lista = criteria.list();	
		return lista;
	}

	@Override
	public List<Matricula> findByPrimaryKey(Object obj) {
		this.sessao =  HibernateUtil.getSession().getCurrentSession();
		Matricula matricula = (Matricula) obj;
		try {
			List<Matricula> lista = this.sessao.createCriteria(Matricula.class)
									.add(Restrictions.eq("idMatricula", matricula.getIdMatricula()))
									.list();
			return lista;
		} catch (Exception e) {
			return null;
		}
	}
	
	@Override
	public List<Matricula> list() {	
		this.sessao =  HibernateUtil.getSession().getCurrentSession();
		try {
			List<Matricula> lista = this.sessao.createCriteria(Matricula.class).list();
			return lista;
		} catch (Exception e) {
			return null;
		}
	}	
}
